package com.stlm2.controller;

import com.stlm2.dems.entity.GoodSpecification;
import com.stlm2.dems.entity.SpecificationContent;
import com.stlm2.dems.service.GoodSpecificationService;
import com.stlm2.dems.service.SpecificationContentService;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author：fuchunyu
 * Date：2017/9/8
 * Description：
 */
@Controller
@RequestMapping("/goodSpecificationC")
public class GoodSpecificationController {

    @Resource
    private GoodSpecificationService goodSpecificationService;
    @Resource
    private SpecificationContentService specificationContentService;
    @ResponseBody
    @RequestMapping(value = "/getGoodSpecification.json",produces="application/json;charset=UTF-8")
    public String getGoodSpecification(String companyCode,Integer goodid,String propertiesid){

        Map<String,Object> map=new HashMap<String,Object>();
        map.put("goodid",goodid);
        map.put("propertiesid",propertiesid);

        GoodSpecification goodSpecification=goodSpecificationService.getGoodSpecification(map);
        if(goodSpecification==null)
            return CodeContent.returnFailure("没有添加此规格组合！");
        return CodeContent.returnSuccess(goodSpecificationService.builderModelAll(goodSpecification));
    }
    //传入规格组合前半部分，判断下属规格有哪些
    @ResponseBody
    @RequestMapping(value = "/getGoodSpecificationContent.json",produces = "application/json;charset=utf-8")
    public String getGoodSpecificationContent(String companyCode,String gooid,String propertiesid){
        if(!NumberUtils.isDigits(gooid))
            return CodeContent.returnFailure("商品ID错误！");
        if(StringUtils.isEmpty(propertiesid))
            return CodeContent.returnFailure("规格组合是空 ！");
        Set set = new HashSet();
        List<List> list=new ArrayList<List>();
        String proper[]=propertiesid.split(",");
        if(proper.length==1) {
            set.addAll(this.getProperties(gooid, propertiesid));
        }
        if(proper.length==2) {
            for (String s:proper)
                set.addAll(this.getProperties(gooid,s));
            set.addAll(this.getProperties(gooid, propertiesid));
        }
        if(proper.length==3){
            String proper1=proper[0]+","+proper[1];
            String proper2=proper[0]+","+proper[2];
            String proper3=proper[1]+","+proper[2];
            set.addAll(this.getProperties(gooid, proper1));
            set.addAll(this.getProperties(gooid, proper2));
            set.addAll(this.getProperties(gooid, proper3));
        }

        return CodeContent.returnSuccess(set);
    }

    @ResponseBody
    @RequestMapping(value = "/getGoodSpecificationContent2.json",produces = "application/json;charset=utf-8")
    public String getGoodSpecificationContent2(String companyCode,String gooid,String propertiesid){
        if(!NumberUtils.isDigits(gooid))
            return CodeContent.returnFailure("商品ID错误！");
        if(StringUtils.isEmpty(propertiesid))
            return CodeContent.returnFailure("规格ID是空 ！");
        GoodSpecification goodSpecification=new GoodSpecification();
        goodSpecification.setGoodId(Integer.parseInt(gooid));
        goodSpecification.setPropertiesId(":"+propertiesid);
        List<Integer> list=new ArrayList<Integer>();
        SpecificationContent specificationContent=specificationContentService.findEntityById(Integer.parseInt(propertiesid));
        List<GoodSpecification> goodSpecificationList=goodSpecificationService.getList1(-1,-1,"query2",goodSpecification);
        List<SpecificationContent> specificationContents=specificationContentService.getList1(-1,-1,"getSpecificationContent2",specificationContent);
        for(SpecificationContent s:specificationContents){
            boolean flag=false;
            for (GoodSpecification g:goodSpecificationList){
                   if(g.getPropertiesId().contains(s.getId().toString())){
                       flag=true;
                       break;
                   }
                }
            if(!flag)
                list.add(s.getId());
        }

        return CodeContent.returnSuccess(list);
    }

    @ResponseBody
    @RequestMapping(value = "/getGoodSpecificationContent3.json",produces = "application/json;charset=utf-8")
    public String getGoodSpecificationContent3(String companyCode,String gooid,String propertiesid){
        if(!NumberUtils.isDigits(gooid))
            return CodeContent.returnFailure("商品ID错误！");
        if(StringUtils.isEmpty(propertiesid))
            return CodeContent.returnFailure("规格ID是空 ！");

        String[] proper=propertiesid.split(",");
        StringBuffer buffer=new StringBuffer();
        for (int i=0;i<=proper.length-1;i++)
            buffer.append(i<proper.length-1?specificationContentService.findEntityById(Integer.parseInt(proper[i])).getSpecificationId()+",":specificationContentService.findEntityById(Integer.parseInt(proper[i])).getSpecificationId());
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("goodId",gooid);
        map.put("specificationId",buffer.toString());

        GoodSpecification goodSpecification=new GoodSpecification();
        goodSpecification.setGoodId(Integer.parseInt(gooid));
        goodSpecification.setPropertiesId(":"+propertiesid);
        List<Integer> list=new ArrayList<Integer>();

        List<GoodSpecification> goodSpecificationList=goodSpecificationService.getList1(-1,-1,"query2",goodSpecification);
        List<SpecificationContent> specificationContents=specificationContentService.getList3(-1,-1,map);
        for(SpecificationContent s:specificationContents){
            boolean flag=false;
            for (GoodSpecification g:goodSpecificationList){
                if(g.getPropertiesId().contains(s.getId().toString())){
                    flag=true;
                    break;
                }
            }
            if(!flag)
                list.add(s.getId());
        }

        return CodeContent.returnSuccess(list);
    }

    public List getProperties(String gooid, String propertiesid){
        String[] proper=propertiesid.split(",");
        StringBuffer buffer=new StringBuffer();
        for (int i=0;i<=proper.length-1;i++)
            buffer.append(i<proper.length-1?proper[i].split(":")[0]+",":proper[i].split(":")[0]);
        Map<String,Object> scmap=new HashMap<String,Object>();
        scmap.put("goodId",gooid);
        scmap.put("specificationId",buffer.toString());
        List<SpecificationContent> specificationContents=specificationContentService.getList3(-1,-1,scmap);

        Map<String,Object> gsmap=new HashMap<String,Object>();
        gsmap.put("goodId",gooid);
        for (int i=0;i<=proper.length-1;i++)
            gsmap.put("propertiesId"+i,proper[i]);
        List list=new ArrayList();
        List<GoodSpecification> goodSpecificationList=goodSpecificationService.getList3(-1,-1,gsmap);
        for(SpecificationContent s:specificationContents){
            boolean flag=false;
            for (GoodSpecification g:goodSpecificationList){
                if(g.getPropertiesId().contains(s.getId().toString())){
                    flag=true;
                    break;
                }
            }
            if(!flag)
                list.add(s.getId());
        }
        return list;
    }
}
